<?php
session_start();
include_once ("../php/connect_mysql.php");
$arr = array('username', 'password1', 'email', 'password2', 'head_pic','captcha');
$values = array();
foreach ($arr as $item) {
    $values["$item"] = $_POST["$item"];
}
$username = $values['username'];
$passwd = $values['password1'];
$email = $values['email'];
$passwd2 =  $values['password2'];
$head_pic = $values['head_pic'];
$captcha = $values['captcha'];

$sql_select = "SELECT * FROM user WHERE username = '$username'";
if($passwd == $passwd2){
    $select = mysqli_query($conn,$sql_select);
    $num = mysqli_num_rows($select);//函数返回结果集中行的数量
    if($num){
        echo "<script>alert('注册失败,已存在用户名！');window.location.href = '../page/register_page.php';</script>";
    }elseif ($_SESSION['string'] == $captcha){
        $passwd_md5 = md5($passwd);
        $sql="insert into user(username,password,email,headpic_path) values('$username','$passwd_md5','$email','$head_pic')";
        $result=mysqli_query($conn,$sql);
        //判断是否注册后显示内容
        if(!$result)
        {
            echo "<script>alert('注册失败！');window.location.href = '../page/register_page.php';</script>";
        }
        else
        {
            echo "<script>alert('注册成功,去登录吧！');window.location.href = '../page/login_page.php';</script>";
        }
    } else{
        echo "<script>alert('验证码错误!');window.location.href = '../page/register_page.php';</script>";
    }
}
else{
    echo "<script>alert('密码不一致!');window.location.href = '../page/register_page.php';</script>";
}


